{% extends 'base.html' %}

{% block title %}登录 - Django RBAC 项目{% endblock %}

{% block content %}
<div class="min-h-screen bg-gray-50 flex flex-col justify-center py-12 sm:px-6 lg:px-8">
    <div class="sm:mx-auto sm:w-full sm:max-w-md">
        <h2 class="mt-6 text-center text-3xl font-extrabold text-gray-900">
            用户登录
        </h2>
        <p class="mt-2 text-center text-sm text-gray-600">
            还没有账号？
            <a href="{% url 'register' %}" class="font-medium text-blue-600 hover:text-blue-500">
                立即注册
            </a>
        </p>
    </div>

    <div class="mt-8 sm:mx-auto sm:w-full sm:max-w-md">
        <div class="bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10">
            <form class="space-y-6" method="post">
                {% csrf_token %}

                {% if form.non_field_errors %}
                <div class="rounded-md bg-red-50 p-4">
                    <div class="flex">
                        <div class="flex-shrink-0">
                            <svg class="h-5 w-5 text-red-400" viewBox="0 0 20 20" fill="currentColor">
                                <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd"/>
                            </svg>
                        </div>
                        <div class="ml-3">
                            <h3 class="text-sm font-medium text-red-800">
                                登录失败
                            </h3>
                            <div class="mt-2 text-sm text-red-700">
                                <ul class="list-disc pl-5 space-y-1">
                                    {% for error in form.non_field_errors %}
                                        <li>{{ error }}</li>
                                    {% endfor %}
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
                {% endif %}

                <div>
                    <label for="{{ form.username.id_for_label }}" class="block text-sm font-medium text-gray-700">
                        用户名
                    </label>
                    <div class="mt-1 relative rounded-md shadow-sm">
                        <input type="text" name="{{ form.username.name }}" id="{{ form.username.id_for_label }}"
                               class="appearance-none block w-full px-3 py-2 border {% if form.username.errors %}border-red-300 text-red-900 placeholder-red-300 focus:ring-red-500 focus:border-red-500{% else %}border-gray-300 focus:ring-blue-500 focus:border-blue-500{% endif %} rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-2 sm:text-sm"
                               placeholder="请输入用户名"
                               value="{{ form.username.value|default:'' }}"
                               autocomplete="username">
                        {% if form.username.errors %}
                        <div class="absolute inset-y-0 right-0 pr-3 flex items-center pointer-events-none">
                            <svg class="h-5 w-5 text-red-500" viewBox="0 0 20 20" fill="currentColor">
                                <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z" clip-rule="evenodd"/>
                            </svg>
                        </div>
                        {% endif %}
                    </div>
                    {% if form.username.errors %}
                    <p class="mt-2 text-sm text-red-600">
                        {% for error in form.username.errors %}{{ error }}{% endfor %}
                    </p>
                    {% endif %}
                </div>

                <div>
                    <label for="{{ form.password.id_for_label }}" class="block text-sm font-medium text-gray-700">
                        密码
                    </label>
                    <div class="mt-1 relative rounded-md shadow-sm">
                        <input type="password" name="{{ form.password.name }}" id="{{ form.password.id_for_label }}"
                               class="appearance-none block w-full px-3 py-2 border {% if form.password.errors %}border-red-300 text-red-900 placeholder-red-300 focus:ring-red-500 focus:border-red-500{% else %}border-gray-300 focus:ring-blue-500 focus:border-blue-500{% endif %} rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-2 sm:text-sm"
                               placeholder="请输入密码"
                               autocomplete="current-password">
                        {% if form.password.errors %}
                        <div class="absolute inset-y-0 right-0 pr-3 flex items-center pointer-events-none">
                            <svg class="h-5 w-5 text-red-500" viewBox="0 0 20 20" fill="currentColor">
                                <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z" clip-rule="evenodd"/>
                            </svg>
                        </div>
                        {% endif %}
                    </div>
                    {% if form.password.errors %}
                    <p class="mt-2 text-sm text-red-600">
                        {% for error in form.password.errors %}{{ error }}{% endfor %}
                    </p>
                    {% endif %}
                </div>

                <div class="flex items-center justify-between">
                    <div class="flex items-center">
                        <input id="remember_me" name="remember_me" type="checkbox"
                               class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded">
                        <label for="remember_me" class="ml-2 block text-sm text-gray-900">
                            记住我
                        </label>
                    </div>

                    <div class="text-sm">
                        <a href="{% url 'forgot_password' %}" class="font-medium text-blue-600 hover:text-blue-500">
                            忘记密码？
                        </a>
                    </div>
                </div>

                <div>
                    <button type="submit"
                            class="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
                        登录
                    </button>
                </div>
            </form>


        </div>
    </div>
</div>
{% endblock %}

